$import("g.Account");

$import("fp.utils.ContentExtractor");

with ($ns("fp.caches"))
{
    fp.caches.FeedCacheClass = function()
    {
        var me = this;
        
        
        me.isEnabled = function()
        {
            return fp.Preferences.get("cache.enabled", "true") != "false";
        }
        
        
        
        var _updateTimer = null;
        me.updateCache = function()
        {
            _clearTimer();
            
            if (!me.isEnabled())
            {
                console.warn("Updating feed cache failed. FeedCache is not enabled.");
                return;
            }
            
            if (navigator.onLine)
            {
                console.log("Updating feed cache...");
                g.Reader.getFeeds({
                    dataType: "text",
                    count: fp.Preferences.get("cache.feedCache.size", 100),
                    noCache: true,
                    
                    success: function(p_result)
                    {
                        _setTimer();
                        
                        fp.Storage.set("cache.feedCache.time", new Date() * 1);
                        fp.Storage.set("cache.feedCache", p_result);
                        
                        _cacheImages($.parseJSON(p_result).items);
                    },
                    error: function()
                    {
                        _setTimer();
                    }
                });
            }
            else
            {
                console.warn("Updating feed cache failed. Chrome is not online.");
            }
        };
        
        
        function _setTimer()
        {
            _clearTimer();
            _updateTimer = setTimeout(me.updateCache, fp.Preferences.get("cache.feedCache.interval", 2) * 60000);
        }
        
        function _clearTimer()
        {
            if (_updateTimer != null)
            {
                clearTimeout(_updateTimer);
                _updateTimer = null;
            }
        }
        
        
        
        function _cacheImages(p_feeds)
        {
            console.log("Caching images...");
            var cacheMode = fp.Preferences.get("cache.imageCache.mode", "thumbnail");
            if (cacheMode == "none")
            {
                return;
            }
            
            for (var i = 0; i < p_feeds.length; i++)
            {
                var feed = p_feeds[i];
                var html = fp.utils.ContentExtractor.extractContent(feed)
                var images = null;
                if (cacheMode == "all")
                {
                    images = fp.utils.ContentExtractor.extractImageUrls(html);
                    fp.caches.ImageCache.cacheImages(images);
                }
                else if (cacheMode == "thumbnail")
                {
                    image = fp.utils.ContentExtractor.extractThumbnailUrl(html);
                    if (image != null)
                    {
                        fp.caches.ImageCache.cacheImage(image);
                    }
                }
            }
        }
        
        return me;
    };
    
    fp.caches.FeedCache = new fp.caches.FeedCacheClass();
}